/**
* 系统管理  管理员管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index/mainAdmin' }">主页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.name" placeholder="输入管理员名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button v-if="adminNameUser==='admin'" size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>

    <!--列表-->
    <el-table size="small" :data="adminData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" sortable prop="adminId" label="管理员id" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="adminName" label="管理员名" width="120">
      </el-table-column>
<!--      <el-table-column align="center" sortable prop="adminPassword" label="密码" width="120">
      </el-table-column>-->
      <el-table-column align="center" sortable prop="adminRole" label="角色" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="adminMobile" label="手机号" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="adminSex" label="性别" min-width="50">
      </el-table-column>
      <el-table-column align="center" sortable prop="adminEmail" label="邮件" min-width="120">
      </el-table-column>
      <el-table-column v-if="adminNameUser==='admin'" label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.$index!=0" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" v-if="scope.$index===0" type="success">超级管理员</el-button>
          <el-button size="mini" v-if="scope.$index!=0" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="管理员名" prop="adminName">
          <el-input size="small" v-model="editForm.adminName" auto-complete="off" placeholder="请输入管理员名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userRealName">
          <el-input size="small" v-model="editForm.adminPassword" auto-complete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="adminRole">
          <el-select size="small" v-model="editForm.adminRole" placeholder="请选择" class="userRole">
            <el-option label="普通管理员" value="普通管理员"></el-option>
            <el-option label="公司用户" value="公司用户"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="userMobile">
          <el-input size="small" v-model="editForm.adminMobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="邮件" prop="userEmail">
          <el-input size="small" v-model="editForm.adminEmail" placeholder="请输入邮箱地址"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="adminSex">
          <el-radio v-model="editForm.adminSex" label="男">男</el-radio>
          <el-radio v-model="editForm.adminSex" label="女">女</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
// 导入请求方法
import Pagination from '../../components/Paginayion'
export default {
  data() {
    return {
      loading: false, //是显示加载
      title: '管理员管理',
      editFormVisible: false, //控制编辑页面显示与隐藏

      adminNameUser:'',//管理员
      adminData:[],
      // 编辑与添加
      editForm: {
        adminId: '',//管理员id
        adminName: '',//管理员名
        adminPassword: '',//密码
        adminRole:'',//角色
        adminMobile: '',//手机号
        adminSex: '',//性别
        adminEmail: '',//邮件
      },
      // rules表单验证
      rules: {
        adminName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        adminPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        adminRole: [{ required: true, message: '请选择角色', trigger: 'blur' }],
        adminMobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
            required: true,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],
        adminEmail: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            required: true,
            message: '请输入正确的邮箱',
            trigger: 'blur'
          }
        ],
        adminSex: [{ required: true, message: '请选择性别', trigger: 'blur' }]
      },
      // 请求数据参数
      formInline: {
        page: 1,
        limit: 10,
        name:'',
        userName: '',
        adminName: ''
      },
      listData: [], //搜索数据
      // 选择数据
      selectdata: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.adminNameUser = localStorage.getItem('adminName')
    this.getdata()
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    getdata() {
      this.loading = true
      this.$axios
          .post(`http://localhost:9090/loan/admin/getAdminList`).then(
          response => {
            this.adminData = response.data;
            this.selectdata = response.data;

            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = this.adminData.length
            this.loading = false
          }
      )
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    //搜索事件
    search() {
      if (this.formInline.name===''){
        this.adminData=this.selectdata
      }else {
        this.adminData=[]
         this.loading = true
        for (let i=0;i<this.selectdata.length;i++){
          if (this.selectdata[i].adminName.indexOf(this.formInline.name)!=-1){
            // alert('收到了')
            // console.log(this.listData[i])
            this.adminData.push(this.selectdata[i])
          }
        }
         this.loading = false
      }
     /* this.getdata(this.formInline)*/
    },

    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改管理员'
        this.editForm.adminId = row.adminId
        this.editForm.adminRole = row.adminRole
        this.editForm.adminName = row.adminName
        this.editForm.adminPassword = row.adminPassword
        this.editForm.adminMobile = row.adminMobile
        this.editForm.adminEmail = row.adminEmail
        this.editForm.adminSex = row.adminSex
      } else {
        this.title = '添加管理员'
        this.editForm.adminId = ''
        this.editForm.adminRole = ''
        this.editForm.adminName = ''
        this.editForm.adminPassword = ''
        this.editForm.adminMobile = ''
        this.editForm.adminEmail = ''
        this.editForm.adminSex = ''
      }
    },
    // 编辑、添加提交方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          // 请求方法
          this.$axios
              .post(`http://localhost:9090/loan/admin/updateAdmin`,this.$qs.stringify(this.editForm)).then(
              response => {
                this.editFormVisible = false
                this.loading = false
                if (response.data.success) {
                  this.getdata(this.formInline)
                  this.$message({
                    type: 'success',
                    message: '数据保存成功！'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: "保存失败，请稍后再试！"
                  })
                }

              }
          ).catch(err => {
            this.editFormVisible = false
            this.loading = false
            this.$message.error('保存失败，请稍后再试！')
          })

        } else {
          return false
        }

      })
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'perm') {
        this.dataAccessshow = false
      } else if (dialog == 'unit') {
        this.unitAccessshow = false
      }
    },
    // 删除用户
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            // 删除
            // 请求方法
            this.$axios
                .post(`http://localhost:9090/loan/admin/deleteAdmin?adminId=`+row.adminId).then(
                response => {
                  if (response.data.success) {
                    this.getdata(this.formInline)
                    this.$message({
                      type: 'success',
                      message: '数据已删除！'
                    })
                    this.getdata()
                  } else {
                    this.$message({
                      type: 'info',
                      message: "数据删除失败，请稍后再试！"
                    })
                  }

                }
            ).catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('数据删除失败，请稍后再试！')
            })

          })
    },

  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

